package com.bleeth.flow.web.model;

import com.bleeth.flow.core.FlowApp;
import com.bleeth.flow.core.common.APlugin;
import com.bleeth.flow.core.common.FlowTask;
import com.bleeth.flow.web.handler.FlowProjectChoiceFetchEruptJobHandler;
import com.bleeth.flow.web.service.FlowService;
import com.bleeth.flow.step.field.FunctionPlugin;
import lombok.Data;
import org.springframework.stereotype.Component;
import xyz.erupt.annotation.Erupt;
import xyz.erupt.annotation.EruptField;
import xyz.erupt.annotation.fun.DataProxy;
import xyz.erupt.annotation.fun.OperationHandler;
import xyz.erupt.annotation.sub_erupt.RowOperation;
import xyz.erupt.annotation.sub_field.Edit;
import xyz.erupt.annotation.sub_field.EditType;
import xyz.erupt.annotation.sub_field.View;
import xyz.erupt.annotation.sub_field.ViewType;
import xyz.erupt.annotation.sub_field.sub_edit.ChoiceType;
import xyz.erupt.annotation.sub_field.sub_edit.CodeEditorType;
import xyz.erupt.annotation.sub_field.sub_edit.Search;

import javax.annotation.Resource;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.List;

/**
 * @author ：Bleeth
 * @date ：2022-05-16
 * @description：兴业数据管理模型
 */

@Erupt(name = "Flow管理",
        dataProxy = FlowInfoModel.class,
        rowOperation = @RowOperation(code = "action", icon = "fa fa-play", title = "执行一次任务", operationHandler = FlowInfoModel.class)

)
@Entity
@Table(name = "flow_info")
@Data
@Component
public class FlowInfoModel extends FlowBaseModel implements DataProxy<FlowInfoModel>, OperationHandler<FlowInfoModel, Void> {

    @EruptField(
            views = @View(title = "Flow名称"),
            edit = @Edit(title = "Flow名称", search = @Search(vague = true))
    )
    private String flowName;


    @EruptField(
            views = @View(title = "FlowUid", sortable = true),
            edit = @Edit(title = "FlowUid", search = @Search(vague = true))
    )
    private String flowUid;


    @EruptField(
            views = @View(title = "项目名称"),
            edit = @Edit(title = "项目名称", desc = "项目名称",
                    choiceType = @ChoiceType(fetchHandler = FlowProjectChoiceFetchEruptJobHandler.class)
                    , notNull = true, search = @Search, type = EditType.CHOICE)
    )
    private String projectName;


    @EruptField(
            views = @View(title = "代码", type = ViewType.CODE),
            edit = @Edit(title = "代码(JSON)", type = EditType.CODE_EDITOR,
                    codeEditType = @CodeEditorType(language = "json"))
    )
    private String flowContent;

    @Transient
    @Resource
    private FlowService flowService;


    @Override
    public String exec(List<FlowInfoModel> flowInfoModels, Void param, String[] operationParam) {
        FlowInfoModel flowInfoModel = flowInfoModels.get(0);
        String flowContentTemplate = flowInfoModel.getFlowContent();
        FlowTask flowTask = FlowApp.parseFlow(flowContentTemplate);
        for (APlugin plugin : flowTask.getPlugins()) {
            if (plugin instanceof FunctionPlugin) {
                FunctionPlugin functionPlugin = (FunctionPlugin) plugin;
                List<FunctionPlugin.FunctionParameter> funcList = functionPlugin.getFuncList();
                for (FunctionPlugin.FunctionParameter functionParameter : funcList) {
                    String funcUid = functionParameter.getFuncUid();
                    FlowFunctionModel flowFunctionModel = flowService.findByFunctionUid(funcUid);
                    if (flowFunctionModel == null) {
                        continue;
                    }
                    String functionContent = flowFunctionModel.getFuncContent();
                    String functionName = flowFunctionModel.getFuncName();
                    functionParameter.setFuncContent(functionContent);
                    functionParameter.setFuncName(functionName);
                }
            }
        }
        flowTask.run();
        return null;
    }
}
